package com.sinosoft.cnmuseum.dao;

import java.util.HashMap;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.sinosoft.commontools.record.Record;
import com.sinosoft.dbtools.operate.DBActorMgr;
import com.sinosoft.dbtools.operate.IDBActor;
import com.sinosoft.dbtools.operate.single.IDBSingleTableActor;
import com.sinosoft.cnmuseum.dao.i.IBaseDao;

public class BaseDao implements IBaseDao {
	protected static Logger logger = LogManager.getLogger(BaseDao.class);
	
	IDBSingleTableActor actor = null;
	
	protected IDBSingleTableActor getActor() {
		return actor;
	}

	@Override
	public List<Record> getListByProperty(HashMap<String, Object> map) {
		try {
			return getActor().findByProperties(map);
		} catch (Exception e) {
		}
		return null;
	}
	@Override
	public List<Record> getListByProperty(Record record) {
		try {
			HashMap<String, Object> map = new HashMap<String, Object>();
			List<String> list = record.getAllFlds();
			for(String str:list){
				map.put(str, record.getValue(str));
			}
			return getActor().findByProperties(map);
		} catch (Exception e) {
		}
		return null;
	}
	
	@Override
	public Record getListByProperty(HashMap<String, Object> map, String sort) {
		try {
			List<Record> list = getActor().findByProperties(map);
			if(list.size()>0){
				return getActor().find4PageByProperties(map, 0, list.size()+1, sort, true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * java.util.HashMap 查询条件
	 * int 当前页
	 * int 每页数量
	 * String 排序字段
	 * boolean 正序（true）倒叙（false）
	 * return 
	 */
	@Override
	public Record getPageByProperty(HashMap<String, Object> map, int pagenumber, int pagesize, String column, boolean isorderby){
		try {
			return getActor().find4PageByProperties(map, pagenumber, pagesize, column, isorderby);
		} catch (Exception e) {
		}
		return null;
	}

	@Override
	public Record getById(int id) {
		try {
			return getActor().findById(id);
		} catch (Exception e) {
		}
		return null;
	}

	@Override
	public boolean insert(Record r) {
		try {
			return getActor().insert(r);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(Record r) {
		try {
			return getActor().update(r);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(Record r) {
		try {
			return getActor().delete(r);
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		return delete(getById(id));
	}

	@Override
	public List<Record> getAll() {
		try {
			return getActor().listAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean insert(List<Record> list) {
		try {
			if(list==null) return true;
			for(Record r:list){
				insert(r);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean update(List<Record> list) {
		try {
			if(list==null) return true;
			for(Record r:list){
				update(r);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean delete(List<Record> list) {
		try {
			if(list==null) return true;
			for(Record r:list){
				delete(r);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public List<Record> getBySql(String sql) {
		try {
			logger.info("excute sql:"+sql);
			IDBActor dbactor = DBActorMgr.getDBActor();
			return dbactor.getRowsForRecord(sql);
		} catch (Exception e) {
			logger.error("excute sql error:"+sql);
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

}
